package com.angel.activity;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.angel.entity.Task;
import com.angel.entity.WillBean;
import com.angel.entity.WillBeanList;
import com.angel.port.IActivity;
import com.angel.util.BaseUtil;
import com.angel.util.StringUtil;
import com.angel.util.UserInfoUtil;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
/*编辑意愿清单类*/
public class WillingMenuEdit extends Activity implements IActivity{
        private Button backBnt,commitBnt;
        private EditText userNameEdit,phoneEdit,willEdit;
        private Spinner areaEdit,typeEdit,timeEdit;
        private MyListener listener;
        private TextView topTarTitle;
        private HashMap<String,String> map=new HashMap<String,String>();
        private ArrayAdapter areaAdapter,typeAdapter,timeAdapter;
        private MyOnItemSelectedListener myItemListener;
        private Intent intent;//用于接收传递的数据
        private String willId,describe;//意愿Id
        private List<WillBean> willList;//返回的意愿内容
        private WillBeanList willBeanList;
        private Context context;
        private ProgressDialog progressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_will);
		 MobclickAgent.onError(this);//自动捕获异常退出（FC ）
		context=WillingMenuEdit.this;
		
		intent=getIntent();
		willId=intent.getStringExtra("willId");
		describe=intent.getStringExtra("describe");
		
		backBnt=(Button)findViewById(R.id.back_bnt);
		commitBnt=(Button)findViewById(R.id.top_right_Bnt_id);
		/*获取编辑框实例*/
		willEdit=(EditText)findViewById(R.id.edit_will_content);
		userNameEdit=(EditText)findViewById(R.id.edit_name);
		phoneEdit=(EditText)findViewById(R.id.edit_phone);
		/*下拉框实例*/
		areaEdit=(Spinner)findViewById(R.id.edit_area);
		typeEdit=(Spinner)findViewById(R.id.edit_type);
		timeEdit=(Spinner)findViewById(R.id.edit_free_time);
		
		topTarTitle=(TextView)findViewById(R.id.top_bar_title_txt_id);
		
		listener=new MyListener();
		backBnt.setOnClickListener(listener);
		commitBnt.setOnClickListener(listener);
		topTarTitle.setText(getResources().getString(R.string.willing_text));
		topTarTitle.setTextColor(android.graphics.Color.WHITE);//设置标题颜色
		/*构造区域下拉框适配器*/
		areaAdapter=ArrayAdapter.createFromResource(this, R.array.area_array, android.R.layout.simple_dropdown_item_1line);
		areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		areaEdit.setAdapter(areaAdapter);
		/*构造活动类型适配器*/
		typeAdapter=ArrayAdapter.createFromResource(this, R.array.type_array, android.R.layout.simple_dropdown_item_1line);
		typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		typeEdit.setAdapter(typeAdapter);
		/*构造活动空闲时间适配器*/
		timeAdapter=ArrayAdapter.createFromResource(this, R.array.free_time_array, android.R.layout.simple_dropdown_item_1line);
		timeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		timeEdit.setAdapter(timeAdapter);
		
		myItemListener=new MyOnItemSelectedListener();
		areaEdit.setOnItemSelectedListener(myItemListener);
		timeEdit.setOnItemSelectedListener(myItemListener);
		typeEdit.setOnItemSelectedListener(myItemListener);
		/*初始化EditText内容*/
		String phoneNum=UserInfoUtil.getValue(context, "phoneAccount", "");
		String userName=UserInfoUtil.getValue(context, "userName", "");
		if(!phoneNum.equals("")){
			phoneEdit.setText(phoneNum);
		}
		if(describe!=null){//如果描述内容不为空，则设置到描述框中
			willEdit.setText(describe);
		}
			userNameEdit.setText(userName);
	}
	class MyOnItemSelectedListener implements OnItemSelectedListener{
		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			int ids=parent.getId();
			if(ids==R.id.edit_area){
				map.put("area", areaEdit.getSelectedItem().toString());
			}else if(ids==R.id.edit_free_time){
				map.put("ideaTime", timeEdit.getSelectedItem().toString());
			}else if(ids== R.id.edit_type){
				map.put("type", typeEdit.getSelectedItem().toString());
			}
		}
		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}
	}
	class MyListener implements OnClickListener{
		@Override
		public void onClick(View v) {
          int id=v.getId();
          if(id==R.id.back_bnt){
        	  finish();
          }else if(id==R.id.top_right_Bnt_id){
        	  String userName=userNameEdit.getText().toString();
        	  String phoneNum=phoneEdit.getText().toString();
        	  String willContnet=willEdit.getText().toString();
        	  /*当意愿信息不为空时才执行*/
        	  if(check(userName,phoneNum,willContnet)){
	        	  map.put("userId", UserInfoUtil.getValue(WillingMenuEdit.this, "userId","1"));
	        	  Log.i("userId","WillingMenuEdit-->userId"+UserInfoUtil.getValue(context, "userId", ""));
	        	  map.put("willId", willId);
	        	  map.put("userName",userName);
	        	  map.put("telNum",phoneNum);
	        	  map.put("willContent",willContnet);
	        	  //提交请求
	        	  BaseUtil.startService(Task.EDIT_WILL_ACTIVITY,WillingMenuEdit.this,map);
	        	  /*显示加载对话框*/
	        	  progressDialog=new ProgressDialog(context);
	      		  progressDialog.setMessage("提交中...");
	      		  progressDialog.show();
        	  }
		}
		}
	}
	/*检查用户名，手机号，意愿*/
	public Boolean check(String userName,String phone,String willContent){
		 if(StringUtil.isBlank(userName)){
   		  BaseUtil.makeToast(WillingMenuEdit.this,"请输入用户名");
   		  return false;
   	  	 }
		 if(!StringUtil.checkPhoneNum(phone)){
			  BaseUtil.makeToast(WillingMenuEdit.this,"请输入正确的手机号");
		  return false;
		 }
		 if(StringUtil.isBlank(willContent)){
			 BaseUtil.makeToast(WillingMenuEdit.this,"请填写您的意愿");
		  return false;
		 }
		 return true;
		 
	}
	@Override
	public void init() {
	}
	@Override
	public void refresh(Object... params) {
		     String isSucess=BaseUtil.getValueFromJSON((String)params[0]);
		    if(isSucess.equals("2")){
			 BaseUtil.makeToast(WillingMenuEdit.this, "对不起,网络连接超时,请稍后重试...");
			  progressDialog.dismiss();
			 return ;
		    }
		    
			Type type=new TypeToken<WillBeanList>(){}.getType();
			willBeanList=(WillBeanList)BaseUtil.getEntityFromJson((String)params[0], type);
			progressDialog.dismiss();
			if(willBeanList.getIsSuccess().equals("1"))
			{BaseUtil.makeToast(WillingMenuEdit.this, "成功编辑意愿清单");}
			else{
				BaseUtil.makeToast(WillingMenuEdit.this, "对不起，网络出现错误,稍后请重试");
			}
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		 MobclickAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		 MobclickAgent.onPause(this);
	}
}
